package com.beifu.manager.service.assetoutplanservice.impl;

import com.beifu.manager.common.Const;
import com.beifu.manager.dao.AssetOutPlanMapper;
import com.beifu.manager.dao.AssetOutTruthMapper;
import com.beifu.manager.dao.AssetTypeMapper;
import com.beifu.manager.model.*;
import com.beifu.manager.service.assetoutplanservice.AssetOutPlanService;
import com.beifu.manager.service.assetouttruthservice.AssetOutTruthService;
import com.beifu.manager.vo.assetout.AssetOutDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: licy
 * @Date :  2018/6/17 20:32
 * @Last Modified by: licy
 * @Last Modified time:  2018/6/17 20:32
 */
@Service
public class AssetOutPlanServiceImpl implements AssetOutPlanService {
    
    @Autowired
    private AssetOutPlanMapper assetOutPlanMapper;
    @Autowired
    private AssetTypeMapper assetTypeMapper;
    @Autowired
    private AssetOutTruthMapper assetOutTruthMapper;
    @Autowired
    private AssetOutTruthService assetOutTruthService;
    @Override
    public AssetOutDetailVo getOnePlanDetail(String assetOutId, boolean washFlag) {
        //获取计划列表
        List<AssetOutPlan> assetOutPlans = getAssetOutPlans(assetOutId, washFlag);
        AssetOutDetailVo assetOutDetailVo  = new AssetOutDetailVo();
        assetOutDetailVo.setAssetOutPlansList(assetOutPlans);
        return assetOutDetailVo;
    }

    public List<AssetOutPlan> getAssetOutPlans(String assetOutId, boolean washFlag) {
        int assetOutPlanid = Integer.valueOf(assetOutId);
        Long assetsOutIdLong = Long.valueOf(assetOutPlanid);
        AssetOutPlanExample assetOutPlanExample = new AssetOutPlanExample();
        assetOutPlanExample.createCriteria().andAssetOutIdEqualTo(assetOutPlanid);
        List<AssetOutPlan> assetOutPlans = assetOutPlanMapper.selectByExample(assetOutPlanExample);
        //获取资产类型
        // TODO 查看是否有相同类型的数据如果有 ，合并
        Map<Integer,AssetOutPlan> hashMap = new HashMap<Integer,AssetOutPlan>();
        for(AssetOutPlan assetOutPlan:assetOutPlans){
            Integer assetTypeId = assetOutPlan.getAssetTypeId();
            AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
            String typeName = assetType.getName();
            assetOutPlan.setTypeName(typeName);
            if(hashMap.get(assetTypeId)==null){
                hashMap.put(assetTypeId ,assetOutPlan );
            }else {
                AssetOutPlan outPlan = hashMap.get(assetTypeId);
                int i = outPlan.getOutCount() + assetOutPlan.getOutCount();
                outPlan.setOutCount(i);
                hashMap.put(assetTypeId ,outPlan );
            }
        }
        List<AssetOutPlan> resultList = new ArrayList<AssetOutPlan>();
        for(AssetOutPlan assetOutPlan:hashMap.values()){
            AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
            AssetOutTruthExample.Criteria criteria = assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong).andSecondScanEqualTo(Const.Second_Scan.YES);
            List<AssetOutTruth> assetOutTruthList = assetOutTruthMapper.selectByExample(assetOutTruthExample);
            Map<String, Object> rfIdwithHashMap = assetOutTruthService.getRfIdwithHashMap(assetOutTruthList);
            HashMap<Integer,Integer> typeNumhashMap = (HashMap<Integer,Integer>)rfIdwithHashMap.get("typeNumhashMap");
            assetOutPlan.setTruthCount(typeNumhashMap.get(assetOutPlan.getAssetTypeId()));
            resultList.add(assetOutPlan);

        }
        return assetOutPlans;
    }

    /**
     * 封装成
     * AssetOutPlan  类型  计划数量  实际数量
     * @param assetOutId
     * @return
     */
    @Override
    public List<AssetOutPlan> dealAssetOutStatus(String assetOutId) {
        int assetOutPlanid = Integer.valueOf(assetOutId);
        Long assetsOutIdLong = Long.valueOf(assetOutPlanid);
        AssetOutPlanExample assetOutPlanExample = new AssetOutPlanExample();
        assetOutPlanExample.createCriteria().andAssetOutIdEqualTo(assetOutPlanid);
        List<AssetOutPlan> assetOutPlans = assetOutPlanMapper.selectByExample(assetOutPlanExample);
        //获取资产类型
        // TODO 查看是否有相同类型的数据如果有 ，合并
        Map<Integer,AssetOutPlan> hashMap = new HashMap<Integer,AssetOutPlan>();
        for(AssetOutPlan assetOutPlan:assetOutPlans){
            Integer assetTypeId = assetOutPlan.getAssetTypeId();
            AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
            String typeName = assetType.getName();
            assetOutPlan.setTypeName(typeName);
            if(hashMap.get(assetTypeId)==null){
                hashMap.put(assetTypeId ,assetOutPlan );
            }else {
                AssetOutPlan outPlan = hashMap.get(assetTypeId);
                int i = outPlan.getOutCount() + assetOutPlan.getOutCount();
                outPlan.setOutCount(i);
                hashMap.put(assetTypeId ,outPlan );
            }
        }
        List<AssetOutPlan> resultList = new ArrayList<AssetOutPlan>();
        for(AssetOutPlan assetOutPlan:hashMap.values()){
            AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
            //不同点：第二次扫描了 : 洗涤厂扫描
            AssetOutTruthExample.Criteria criteria = assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong).andSecondScanEqualTo(Const.Wash_Status.YES);
            List<AssetOutTruth> assetOutTruthList = assetOutTruthMapper.selectByExample(assetOutTruthExample);
            Map<String, Object> rfIdwithHashMap = assetOutTruthService.getRfIdwithHashMap(assetOutTruthList);
            HashMap<Integer,Integer> typeNumhashMap = (HashMap<Integer,Integer>)rfIdwithHashMap.get("typeNumhashMap");
            assetOutPlan.setTruthCount(typeNumhashMap.get(assetOutPlan.getAssetTypeId()));
            resultList.add(assetOutPlan);

        }
        return resultList;
    }

    @Override
    public AssetOutDetailVo getOnePlanDetailOut(String assetOutId, boolean washFlag) {
        //获取计划列表
        List<AssetOutPlan> assetOutPlans = getAssetOutPlansOut(assetOutId, washFlag);
        AssetOutDetailVo assetOutDetailVo  = new AssetOutDetailVo();
        assetOutDetailVo.setAssetOutPlansList(assetOutPlans);
        return assetOutDetailVo;
    }
    @Override
    public List<AssetOutPlan> getAssetOutPlansOut(String assetOutId, boolean washFlag) {
        int assetOutPlanid = Integer.valueOf(assetOutId);
        Long assetsOutIdLong = Long.valueOf(assetOutPlanid);
        AssetOutPlanExample assetOutPlanExample = new AssetOutPlanExample();
        assetOutPlanExample.createCriteria().andAssetOutIdEqualTo(assetOutPlanid);
        List<AssetOutPlan> assetOutPlans = assetOutPlanMapper.selectByExample(assetOutPlanExample);
        //获取资产类型
        // TODO 查看是否有相同类型的数据如果有 ，合并
        Map<Integer,AssetOutPlan> hashMap = new HashMap<Integer,AssetOutPlan>();
        for(AssetOutPlan assetOutPlan:assetOutPlans){
            Integer assetTypeId = assetOutPlan.getAssetTypeId();
            AssetType assetType = assetTypeMapper.selectByPrimaryKey(assetTypeId);
            String typeName = assetType.getName();
            assetOutPlan.setTypeName(typeName);
            if(hashMap.get(assetTypeId)==null){
                hashMap.put(assetTypeId ,assetOutPlan );
            }else {
                AssetOutPlan outPlan = hashMap.get(assetTypeId);
                int i = outPlan.getOutCount() + assetOutPlan.getOutCount();
                outPlan.setOutCount(i);
                hashMap.put(assetTypeId ,outPlan );
            }
        }
        List<AssetOutPlan> resultList = new ArrayList<AssetOutPlan>();
        for(AssetOutPlan assetOutPlan:hashMap.values()){
            AssetOutTruthExample assetOutTruthExample = new AssetOutTruthExample();
            //不同点：第二次扫描了 : 出库厂 扫描
            AssetOutTruthExample.Criteria criteria = assetOutTruthExample.createCriteria().andAssetOutIdEqualTo(assetsOutIdLong).andSecondScanEqualTo(Const.Second_Scan.YES);
            List<AssetOutTruth> assetOutTruthList = assetOutTruthMapper.selectByExample(assetOutTruthExample);
            Map<String, Object> rfIdwithHashMap = assetOutTruthService.getRfIdwithHashMap(assetOutTruthList);
            HashMap<Integer,Integer> typeNumhashMap = (HashMap<Integer,Integer>)rfIdwithHashMap.get("typeNumhashMap");
            assetOutPlan.setTruthCount(typeNumhashMap.get(assetOutPlan.getAssetTypeId()));
            resultList.add(assetOutPlan);

        }
        return resultList;
    }
}